package com.yiwei.jmx.view;

/**
 * Created by xsooy-pc on 18-8-29.
 */

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.yiwei.jmx.R;
import com.yiwei.jmx.adapter.ConfirmPopupWindowAdapter;
import com.yiwei.jmx.api.model.PrizeIndexModel;

import java.util.List;

import butterknife.ButterKnife;


/**
 * 弹窗视图
 */
public class ConfirmPopWindow extends PopupWindow {
    private Context context;
    private ConfirmPopupWindowAdapter adapter;

    public ConfirmPopWindow(Context context) {
        super(context);
        this.context = context;
        initalize();
    }

    private void initalize() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_popup_confirm, null);
        adapter = new ConfirmPopupWindowAdapter(context);
        adapter.setOnChooseListener(new ConfirmPopupWindowAdapter.OnChooseListener() {
            @Override
            public void onSuccess(String number) {
                if (onChooseListener != null) {
                    onChooseListener.onSuccess(number);
                    dismiss();
                }
            }
        });
        RecyclerView list = (RecyclerView) view.findViewById(R.id.list);
        list.setAdapter(adapter);
        setContentView(view);
        initWindow();
    }

    public void setData(List<PrizeIndexModel.DataBean.WeekListBean> data) {
        adapter.update(data);
    }


    private void initWindow() {
        DisplayMetrics d = context.getResources().getDisplayMetrics();
//        this.setWidth((int) (d.widthPixels * 0.35));
//        this.setHeight(LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.update();
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
//        backgroundAlpha((Activity) context, 0.8f);//0.0-1.0
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
//                backgroundAlpha((Activity) context, 1f);
            }
        });


    }

    //设置添加屏幕的背景透明度
    public void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }

    public void showAtBottom(View view) {
        //弹窗位置设置
        showAsDropDown(view, Math.abs((view.getWidth() - getWidth()) / 2), 10);
        //showAtLocation(view, Gravity.TOP | Gravity.RIGHT, 10, 110);//有偏差
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onSuccess(String number);
    }

}
